package com.wh1200.mybatis.xmlless.constant


/**
 *
 * Created by 吴昊 on 2018-12-14.
 *
 * @author 吴昊
 * @since 0.0.8
 */
/**  insert 语句模板 */
const val BATCH_INSERT = """INSERT INTO
  %s(
%s
  )
VALUES
  <foreach collection="list" item="item" separator=",">
    (%s)
  </foreach>"""

const val BATCH_INSERT_OR_UPDATE = """INSERT INTO
  %s(%s)
VALUES
  <foreach collection="list" item="item" separator=",">
    (%s)
  </foreach>
ON DUPLICATE KEY UPDATE
  %s 
"""

/**  删除语句模板 */
const val DELETE = """DELETE FROM
  %s
%s
%s"""

/**  单条数据插入语句模板 */
const val INSERT = """INSERT INTO
  %s(%s)
VALUES
  (%s)"""

/**  单条数据插入或更新语句模板 */
const val INSERT_OR_UPDATE = """INSERT INTO
  %s(%s)
VALUES 
  (%s)
ON DUPLICATE KEY UPDATE
  %s"""

/**  join表达式模板 */
const val JOIN = """
  %S JOIN
    %s
  ON
    %s"""

/**  limit 语句模板 */
const val LIMIT = "LIMIT #{%s}, #{%s}"

const val SPRING_PAGEABLE_LIMIT = """<if test="%s.isPaged()">$LIMIT</if>"""

const val PGSQL_LIMIT = "LIMIT #{%s} OFFSET #{%s}"

const val PGSQL_PAGEABLE_LIMIT = """<if test="%s.isPaged()">$PGSQL_LIMIT</if>"""

/**  Pageable参数中的排序 */
const val PAGEABLE_SORT = """<if test="%s.sort.isSorted">
  <foreach collection="%s.sort.get().toArray()" item="item" separator=",">
    ${'$'}{item.property} <if test="item.isAscending">ASC</if><if test="item.isDescending">DESC</if>
  </foreach>
</if>"""

/**  查询语句模板 */
const val SELECT = """SELECT
  %s
FROM
  %s %s
%s 
%s %s
%s
%s"""


const val UPDATE_NO_APPEND = """UPDATE
  %s
%s
  %s"""


/**  更新语句模板 */
const val UPDATE = """UPDATE
  %s
  %s
  %s
%s"""

const val TRIM_CONDITION_CONNECTOR = """<trim suffixOverrides=" AND">
    <trim suffixOverrides=" OR">
%s
    </trim>
  </trim>"""

const val HAVING = """HAVING $TRIM_CONDITION_CONNECTOR"""

/**  where条件模板 */
const val WHERE = """<where>
  $TRIM_CONDITION_CONNECTOR
</where>"""

const val WHERE_WITHOUT_TRIM = """WHERE
%s"""

const val ORDER_BY = """<trim suffixOverrides="ORDER BY">
  ORDER BY
  <trim suffixOverrides=",">
    <trim>
      %s
    </trim>
  </trim>
</trim>
"""

/** 当字段值为空时，插入系统时间 */
const val SYSDATE_INSERT_TEMPLATE = """<choose>
      <when test="%s != null">%s</when>
      <otherwise>CURRENT_TIMESTAMP</otherwise>
    </choose>"""

/** 当字段值为空时，插入当前用户ID */
const val CURRENT_USER_INSERT_TEMPLATE = """<choose>
      <when test="%s != null">%s</when>
      <otherwise>${Strings.CURRENT_USER_HOLDER}</otherwise>
    </choose>"""
